1566B - MIN-MEX Cut - CodeForces Solution


bitmasks constructive algorithms dp greedy *800

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    s = input()
    w = list(s)
    a, b, c = 0, 0, 0

    for i in range(len(s)):
        if s[i] == '0':
            if b > 0:
                c += 1
                break
            a += 1

        else:
            if a > 0:
                b += 1

    if w.count('1') == len(s):
        print(0)
    elif c == 1:
        print(2)
    else:
        print(1)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff first
#define ss second
#define pb push_back
#define mpair make_pair
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef map<ll, ll> mp;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Priyanshu cout.tie(NULL);

// __builtin_ffs(x) (Find First Set) which returns (the index of the least significant bits of x) + 1.
// __lg(x) which returns the index of the highest set bit.

void solve()
{
    string s;
    cin >> s ;
    int zc=count(s.begin(),s.end(),'0');
    if(zc==0) 
    {
        cout<<0<<endl;
        return;
    }
    int ffi =s.find('0');
    int lfi= s.rfind('0');
    if(lfi-ffi+1==zc)
    {
        cout<<1<<endl;
        return;
    }
    else
    {
        cout<<2<<endl;
        return;
    }
}
int main()
{
    Code By Priyanshu
        ll t;
    cin >> t;
    while (t--)
    {
        solve();
        
    }
   
    return 0;
}


Comments

Submit
0 Comments
More Questions

1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger